package com.lncd.backend.controller;

import com.lncd.backend.dto.GeoFenceDTO;
import com.lncd.backend.service.GeoFenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/geofences")
public class GeoFenceController {

    private final GeoFenceService geoFenceService;

    @Autowired
    public GeoFenceController(GeoFenceService geoFenceService) {
        this.geoFenceService = geoFenceService;
    }

    @PostMapping
    public ResponseEntity<GeoFenceDTO> createGeoFence(@RequestBody GeoFenceDTO geoFenceDTO) {
        try {
            GeoFenceDTO createdGeoFence = geoFenceService.createGeoFence(geoFenceDTO);
            return ResponseEntity.ok(createdGeoFence);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<GeoFenceDTO> getGeoFenceById(@PathVariable Long id) {
        try {
            GeoFenceDTO geoFence = geoFenceService.getGeoFenceById(id);
            return ResponseEntity.ok(geoFence);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    @GetMapping("/creator/{creatorId}")
    public ResponseEntity<List<GeoFenceDTO>> getGeoFencesByCreatorId(@PathVariable Long creatorId) {
        try {
            List<GeoFenceDTO> geoFences = geoFenceService.getGeoFencesByCreatorId(creatorId);
            return ResponseEntity.ok(geoFences);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    @GetMapping("/target/{targetUserId}")
    public ResponseEntity<List<GeoFenceDTO>> getGeoFencesByTargetUserId(@PathVariable Long targetUserId) {
        try {
            List<GeoFenceDTO> geoFences = geoFenceService.getGeoFencesByTargetUserId(targetUserId);
            return ResponseEntity.ok(geoFences);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<GeoFenceDTO> updateGeoFence(@PathVariable Long id, @RequestBody GeoFenceDTO geoFenceDTO) {
        try {
            GeoFenceDTO updatedGeoFence = geoFenceService.updateGeoFence(id, geoFenceDTO);
            return ResponseEntity.ok(updatedGeoFence);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, String>> deleteGeoFence(@PathVariable Long id) {
        try {
            geoFenceService.deleteGeoFence(id);
            Map<String, String> response = new HashMap<>();
            response.put("message", "地理栅栏删除成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            Map<String, String> response = new HashMap<>();
            response.put("error", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    @GetMapping("/creator/{creatorId}/active")
    public ResponseEntity<List<GeoFenceDTO>> getActiveGeoFencesByCreatorId(@PathVariable Long creatorId) {
        try {
            List<GeoFenceDTO> geoFences = geoFenceService.getActiveGeoFencesByCreatorId(creatorId);
            return ResponseEntity.ok(geoFences);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    @GetMapping("/target/{targetUserId}/active")
    public ResponseEntity<List<GeoFenceDTO>> getActiveGeoFencesByTargetUserId(@PathVariable Long targetUserId) {
        try {
            List<GeoFenceDTO> geoFences = geoFenceService.getActiveGeoFencesByTargetUserId(targetUserId);
            return ResponseEntity.ok(geoFences);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }
} 